package com.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.dto.PageDTO;
import com.boot.entity.Result;
import com.boot.entity.SysPermission;
import com.boot.service.ISysPermissionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sys-permission")
public class SysPermissionController {

    @Resource
    private ISysPermissionService permissionService;

    @GetMapping("/page")
    public Result<Page<SysPermission>> page(PageDTO pageDTO) {
        Page<SysPermission> page = new Page<>(pageDTO.getPage(), pageDTO.getSize());
        Page<SysPermission> permPage = permissionService.page(page);
        return Result.success(permPage);
    }

    @GetMapping("/menus")
    public Result<List<SysPermission>> menus() {
        LambdaQueryWrapper<SysPermission> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysPermission::getType, "menu");
        return Result.success(permissionService.list(wrapper));
    }

    @GetMapping
    public Result<List<SysPermission>> list() {
        return Result.success(permissionService.list());
    }

    @GetMapping("/{id}")
    public Result<SysPermission> getById(@PathVariable Long id) {
        SysPermission perm = permissionService.getById(id);
        if (perm == null) return Result.fail("权限不存在");
        return Result.success(perm);
    }

    @PostMapping
    public Result<String> add(@Valid @RequestBody SysPermission permission) {
        if (permission.getName() == null || permission.getName().trim().isEmpty()) {
            return Result.fail("权限名称不能为空");
        }
        if (permission.getCode() == null || permission.getCode().trim().isEmpty()) {
            return Result.fail("权限编码不能为空");
        }

        if (permission.getParentId() == null) {
            permission.setParentId(0L);
        }
        permission.setStatus((byte) 1);

        if (permissionService.save(permission)) {
            return Result.success("添加成功");
        }
        return Result.fail("添加失败");
    }

    @PutMapping
    public Result<String> update(@RequestBody SysPermission permission) {
        if (permissionService.updateById(permission)) {
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        removePermissionAndChildren(id);
        return Result.success("删除成功");
    }

    private void removePermissionAndChildren(Long id) {
        LambdaQueryWrapper<SysPermission> wrapper = Wrappers.<SysPermission>lambdaQuery()
                .eq(SysPermission::getParentId, id);
        List<SysPermission> children = permissionService.list(wrapper);
        children.forEach(child -> removePermissionAndChildren(child.getId()));
        permissionService.removeById(id);
    }
}
